/**
 * 全局模块-牌型判断逻辑
 * @author none
 *
 */
class ZJHCardLogic {
    private static _instance: ZJHCardLogic;
    public constructor() {
        if (ZJHCardLogic._instance) {
            error("what the fuck?");
            return;
        }
    }
    public static getInstance(): ZJHCardLogic {
        if (!this._instance) {
            this._instance = new ZJHCardLogic();
        }
        return this._instance;
    }
    /**
 	* 最多人数
	* @type {Number}
	*/
	public static MAX_PLAYER: number = 5;
    /** 花色掩码 **/
    public MASK_COLOR: number = 0xF0;
    /** 数值掩码 **/
    public MASK_VALUE: number = 0x0F;
    /** 扑克数据 **/
    private cardDataArray: number[] = [
        0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, //方块 A - K
        0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, //梅花 A - K
        0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, //红桃 A - K
        0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, //黑桃 A - K
    ];
    /** 获取数值 **/
    public getCardValue(cardData: number): number {
        return (cardData & this.MASK_VALUE);
    }
    /** 获取花色 **/
    public getCardColor(cardData: number): number {
        return (cardData & this.MASK_COLOR) / 16;
    }
    public getCardType(cardData: number[]): number {
        var cardValues: number[] = [];
        for (var i = 0; i < cardData.length; i++) {
            var card = this.getCardValue(cardData[i]);
            cardValues.push(card);
        }
        cardValues.sort(function (a, b)//从小到大排序
        {
            return a - b;
        });
        //判断炸弹
        if (cardValues[0] == cardValues[1] && cardValues[1] == cardValues[2]) {
            return ZJHCardType.THREE;
        }
        //判断顺金
        if (this.getShunZi(cardValues) && this.getTonghua(cardData)) {
            return ZJHCardType.STRAIGHTLINE;
        }
        //判断金花
        if (this.getTonghua(cardData)) {
            return ZJHCardType.STRAIGHT;
        }
        //判断顺子
        if (this.getShunZi(cardValues)) {
            return ZJHCardType.LINE;
        }
        //判断对子
        if (this.getDouble(cardValues)) {
            return ZJHCardType.DOUBLE;
        }
        //单张
        return ZJHCardType.SINGLE;
    }
    private getDouble(cardData: number[]): boolean {
        if (cardData[0] == cardData[1] || cardData[0] == cardData[2] || cardData[1] == cardData[2]) {
            return true
        }
        else {
            return false;
        }
    }
    private getTonghua(cardData: number[]): boolean {
        var cardColor = [];
        for (var i = 0; i < cardData.length; i++) {
            var color = this.getCardColor(cardData[i]);
            cardColor.push(color);
        }
        if (cardColor[0] == cardColor[1] && cardColor[1] == cardColor[2]) {
            return true;
        }
        else {
            return false;
        }
    }
    private getShunZi(cardData: number[]): boolean {
        if (this.getIsA(cardData)) {
            if (cardData[1] == 2 && cardData[2] == 3 || cardData[1] == 12 && cardData[2] == 13) {
                return true
            }
            else {
                return false;
            }
        } else {
            if ((cardData[0] + 1) == cardData[1] && (cardData[1] + 1) == cardData[2]) {
                return true
            }
            else {
                return false;
            }
        }
    }
    private getIsA(cardData: number[]): boolean {
        for (var i = 0; i < cardData.length; i++) {
            if (cardData[i] == 1) {
                return true;
            }
            else {
                return false;
            }
        }
    }
}